package com.cz.modules.jxc.service.impl;

import com.cz.common.entity.BaseTree;
import com.cz.common.utils.Constant;
import com.cz.modules.sys.entity.SysDeptEntity;
import com.cz.modules.sys.entity.SysUserEntity;
import com.cz.modules.sys.service.SysDeptService;
import com.cz.modules.sys.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cz.common.utils.PageUtils;
import com.cz.common.utils.Query;

import com.cz.modules.jxc.dao.JxcShopDao;
import com.cz.modules.jxc.entity.JxcShopEntity;
import com.cz.modules.jxc.service.JxcShopService;


@Service("jxcShopService")
public class JxcShopServiceImpl extends ServiceImpl<JxcShopDao, JxcShopEntity> implements JxcShopService {
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private SysUserService sysUserService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String shopName= (String) params.get("shopName");
        IPage<JxcShopEntity> page = this.page(
                new Query<JxcShopEntity>().getPage(params),
                new QueryWrapper<JxcShopEntity>()
                        .like(StringUtils.isNotBlank(shopName),"shopName",shopName)
                        .apply(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER))
        );
        List<SysUserEntity> userlist=sysUserService.list();
        List<SysDeptEntity> deptlist=sysDeptService.list();
        List<JxcShopEntity> list=page.getRecords();
        for (JxcShopEntity shop:list) {
            for (SysUserEntity user:userlist) {
                if(shop.getCreator().equals(user.getUserId().toString())){
                    shop.setCreatorName(user.getUsername());
                }
                if(!"".equals(shop.getUpdater()) && shop.getUpdater() !=null){
                    if(shop.getUpdater().equals(user.getUserId().toString())){
                        shop.setUpdaterName(user.getUsername());
                    }
                }
            }
            for (SysDeptEntity dept:deptlist) {
                if(shop.getDeptId().equals(dept.getDeptId().toString())){
                    shop.setDeptName(dept.getName());
                }
            }
        }
        return new PageUtils(page);
    }

    @Override
    public List<BaseTree> deptTree() {
        List<BaseTree> baseTree=new ArrayList<>();
        List<SysDeptEntity> deptlist=sysDeptService.list();//所有部门
        for (SysDeptEntity dept:deptlist) {
            if("0".equals(dept.getParentId())){
              BaseTree tree=new BaseTree();
              tree.setId(dept.getDeptId().toString());
              tree.setParentId("0");
              tree.setName(dept.getName());
              baseTree.add(tree);
            }else{
                BaseTree tree=new BaseTree();
                tree.setName(dept.getName());
                tree.setId(dept.getDeptId().toString());
                tree.setParentId(dept.getParentId().toString());
                baseTree.add(tree);
            }
        }
        return baseTree;
    }

}
